package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmDatasource;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmDatasourceEnv;
import com.harmontronics.hanma.paas.paas.dto.input.HmDatasourceCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmDatasourceUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmDatasourceEnvResult;
import com.harmontronics.hanma.paas.paas.dto.output.HmDatasourceResult;
import com.harmontronics.hanma.paas.paas.service.HmDatasourceAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;


/**
 * 数据源
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-05-12 11:12:31
 */
@RestController
@RequestMapping("/api/hmdatasource")
@Api(tags = {"数据源"})
public class HmDatasourceController {
    @Autowired
    private HmDatasourceAppService hmDatasourceappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmDatasourceResult>> list() {
        List<HmDatasource> hmDatasourceList = hmDatasourceappService.getList();

        return ResponseEntity.ok(Convert.toList(HmDatasourceResult.class, hmDatasourceList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmDatasourceResult>> list(
            @ApiParam("KEY") @RequestParam(required = false) String key,
            @ApiParam("名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmDatasource> hmDatasourcePageBase = hmDatasourceappService.getList(key, name, pageNo, pageSize);
        PageBase<HmDatasourceResult> resultPageBase = PageBase.copy(hmDatasourcePageBase, HmDatasourceResult.class);
        for (HmDatasourceResult datasourceResult : resultPageBase.getData()) {
            Optional<HmDatasource> datasource = hmDatasourcePageBase.getData().stream().filter(e -> e.getId().longValue() == datasourceResult.getId().longValue()).findFirst();
            datasource.ifPresent(hmDatasource -> datasourceResult.setEnvList(Convert.toList(HmDatasourceEnvResult.class, hmDatasource.getEnvList())));
        }
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmDatasourceResult> info(@RequestParam("id") Long id) {
        HmDatasource hmDatasource = hmDatasourceappService.findById(id);
        HmDatasourceResult result = BeanUtil.toBean(hmDatasource, HmDatasourceResult.class);
        result.setEnvList(Convert.toList(HmDatasourceEnvResult.class, hmDatasource.getEnvList()));
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmDatasourceCreateInput hmDatasourceInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmDatasource hmDatasource = BeanUtil.toBean(hmDatasourceInput, HmDatasource.class);
        List<HmDatasourceEnv> envs = Convert.toList(HmDatasourceEnv.class, hmDatasourceInput.getEnvList());
        hmDatasourceappService.save(hmDatasource, envs);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmDatasourceUpdateInput hmDatasourceInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmDatasource hmDatasource = BeanUtil.toBean(hmDatasourceInput, HmDatasource.class);
        List<HmDatasourceEnv> envs = Convert.toList(HmDatasourceEnv.class, hmDatasourceInput.getEnvList());
        hmDatasourceappService.update(hmDatasource, envs);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmDatasourceappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
